<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li class="active"><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. cookie是什么">1. cookie是什么</a></li><li><a href="#t12. Cookie的处理流程">2. Cookie的处理流程</a></li><li><a href="#t23. 使用步骤">3. 使用步骤</a><ul><li><a href="#t33.1 服务器发送cookie">3.1 服务器发送cookie</a></li><li><a href="#t43.2 客户端接收保存cookie">3.2 客户端接收保存cookie</a></li><li><a href="#t53.3 客户端发送cookie">3.3 客户端发送cookie</a></li></ul></li><li><a href="#t64. cookie重要属性">4. cookie重要属性</a></li><li><a href="#t75. 在express中向客户端发送cookie">5. 在express中向客户端发送cookie</a><ul><li><a href="#t85.1 设置cookie">5.1 设置cookie</a></li><li><a href="#t95.2 获取cookie">5.2 获取cookie</a></li><li><a href="#t105.3 cookie原理解析">5.3 cookie原理解析</a></li></ul></li><li><a href="#t116. 权限控制">6. 权限控制</a></li><li><a href="#t127.cookie使用注意事项">7.cookie使用注意事项</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. cookie&#x662F;&#x4EC0;&#x4E48;">1. cookie&#x662F;&#x4EC0;&#x4E48; <a href="#t01. cookie&#x662F;&#x4EC0;&#x4E48;"> # </a></h2>
<ul>
<li>HTTP1.0&#x4E2D;&#x534F;&#x8BAE;&#x662F;&#x65E0;&#x72B6;&#x6001;&#x7684;&#xFF0C;&#x4F46;&#x5728;WEB&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x5728;&#x591A;&#x4E2A;&#x8BF7;&#x6C42;&#x4E4B;&#x95F4;&#x5171;&#x4EAB;&#x4F1A;&#x8BDD;&#x662F;&#x975E;&#x5E38;&#x5FC5;&#x8981;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x51FA;&#x73B0;&#x4E86;Cookie</li>
<li>cookie&#x662F;&#x4E3A;&#x4E86;&#x8FA9;&#x522B;&#x7528;&#x6237;&#x8EAB;&#x4EFD;&#xFF0C;&#x8FDB;&#x884C;&#x4F1A;&#x8BDD;&#x8DDF;&#x8E2A;&#x800C;<em>&#x5B58;&#x50A8;&#x5728;&#x5BA2;&#x6237;&#x7AEF;</em>&#x4E0A;&#x7684;&#x6570;&#x636E;</li>
</ul>
<h2 id="t12. Cookie&#x7684;&#x5904;&#x7406;&#x6D41;&#x7A0B;">2. Cookie&#x7684;&#x5904;&#x7406;&#x6D41;&#x7A0B; <a href="#t12. Cookie&#x7684;&#x5904;&#x7406;&#x6D41;&#x7A0B;"> # </a></h2>
<p><img src="http://7xjf2l.com1.z0.glb.clouddn.com/cookies.png" class="img-responsive"></p>
<h2 id="t23. &#x4F7F;&#x7528;&#x6B65;&#x9AA4;">3. &#x4F7F;&#x7528;&#x6B65;&#x9AA4; <a href="#t23. &#x4F7F;&#x7528;&#x6B65;&#x9AA4;"> # </a></h2>
<h3 id="t33.1 &#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;cookie">3.1 &#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;cookie <a href="#t33.1 &#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;cookie"> # </a></h3>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x7B2C;&#x4E00;&#x6B21;&#x8BBF;&#x95EE;&#x670D;&#x52A1;&#x5668;&#x7684;&#x65F6;&#x5019;&#x670D;&#x52A1;&#x5668;&#x901A;&#x8FC7;&#x54CD;&#x5E94;&#x5934;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;Cookie,&#x5C5E;&#x6027;&#x4E4B;&#x95F4;&#x7528;&#x5206;&#x53F7;&#x7A7A;&#x683C;&#x5206;&#x9694;</p>
<pre><code class="lang-javascript">Set-Cookie:name=zfpx; Path=/
</code></pre>
<h3 id="t43.2 &#x5BA2;&#x6237;&#x7AEF;&#x63A5;&#x6536;&#x4FDD;&#x5B58;cookie">3.2 &#x5BA2;&#x6237;&#x7AEF;&#x63A5;&#x6536;&#x4FDD;&#x5B58;cookie <a href="#t43.2 &#x5BA2;&#x6237;&#x7AEF;&#x63A5;&#x6536;&#x4FDD;&#x5B58;cookie"> # </a></h3>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x63A5;&#x6536;&#x5230;Cookie&#x4E4B;&#x540E;&#x4FDD;&#x5B58;&#x5728;&#x672C;&#x5730;</p>
<p><img src="http://7xjf2l.com1.z0.glb.clouddn.com/localcookie.jpg" class="img-responsive"></p>
<h3 id="t53.3 &#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;cookie">3.3 &#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;cookie <a href="#t53.3 &#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;cookie"> # </a></h3>
<p>&#x4EE5;&#x540E;&#x5BA2;&#x6237;&#x7AEF;&#x518D;&#x8BF7;&#x6C42;&#x670D;&#x52A1;&#x5668;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x628A;&#x6B64;Cookie&#x53D1;&#x9001;&#x5230;&#x670D;&#x52A1;&#x5668;&#x7AEF;</p>
<pre><code class="lang-javascript">Cookie:name=zfpx
</code></pre>
<h2 id="t64. cookie&#x91CD;&#x8981;&#x5C5E;&#x6027;">4. cookie&#x91CD;&#x8981;&#x5C5E;&#x6027; <a href="#t64. cookie&#x91CD;&#x8981;&#x5C5E;&#x6027;"> # </a></h2>
<table>
<thead>
<tr>
<th style="text-align:left">&#x5C5E;&#x6027;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">name=value</td>
<td style="text-align:left">&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x8981;&#x4FDD;&#x5B58;&#x7684; Key/Value</td>
</tr>
<tr>
<td style="text-align:left">Domain</td>
<td style="text-align:left">&#x57DF;&#x540D;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;&#x5F53;&#x524D;&#x57DF;&#x540D;</td>
</tr>
<tr>
<td style="text-align:left">maxAge</td>
<td style="text-align:left">&#x6700;&#x5927;&#x5931;&#x6548;&#x65F6;&#x95F4;(&#x6BEB;&#x79D2;),&#x8BBE;&#x7F6E;&#x5728;&#x591A;&#x5C11;&#x540E;&#x5931;&#x6548;</td>
</tr>
<tr>
<td style="text-align:left">secure</td>
<td style="text-align:left">&#x5F53; secure &#x503C;&#x4E3A; true &#x65F6;&#xFF0C;cookie &#x5728; HTTP &#x4E2D;&#x662F;&#x65E0;&#x6548;&#xFF0C;&#x5728; HTTPS &#x4E2D;&#x624D;&#x6709;&#x6548;</td>
</tr>
<tr>
<td style="text-align:left">Path</td>
<td style="text-align:left">&#x8868;&#x793A; cookie &#x5F71;&#x54CD;&#x5230;&#x7684;&#x8DEF;&#xFF0C;&#x5982; path=/&#x3002;&#x5982;&#x679C;&#x8DEF;&#x5F84;&#x4E0D;&#x80FD;&#x5339;&#x914D;&#x65F6;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x5219;&#x4E0D;&#x53D1;&#x9001;&#x8FD9;&#x4E2A;Cookie</td>
</tr>
<tr>
<td style="text-align:left">Expires</td>
<td style="text-align:left">&#x8FC7;&#x671F;&#x65F6;&#x95F4;(&#x79D2;)&#xFF0C;&#x5728;&#x8BBE;&#x7F6E;&#x7684;&#x67D0;&#x4E2A;&#x65F6;&#x95F4;&#x70B9;&#x540E;&#x8BE5; Cookie &#x5C31;&#x4F1A;&#x5931;&#x6548;&#xFF0C;&#x5982; expires=Money, 05-Dec-11 11:11:11 GMT</td>
</tr>
<tr>
<td style="text-align:left">httpOnly</td>
<td style="text-align:left">&#x5982;&#x679C;&#x5728;COOKIE&#x4E2D;&#x8BBE;&#x7F6E;&#x4E86;<code>httpOnly</code>&#x5C5E;&#x6027;&#xFF0C;&#x5219;&#x901A;&#x8FC7;&#x7A0B;&#x5E8F;(JS&#x811A;&#x672C;)&#x5C06;&#x65E0;&#x6CD5;&#x8BFB;&#x53D6;&#x5230;COOKIE&#x4FE1;&#x606F;&#xFF0C;&#x9632;&#x6B62;XSS&#x653B;&#x51FB;&#x4EA7;&#x751F;</td>
</tr>
</tbody>
</table>
<h2 id="t75. &#x5728;express&#x4E2D;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;cookie">5. &#x5728;express&#x4E2D;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;cookie <a href="#t75. &#x5728;express&#x4E2D;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;cookie"> # </a></h2>
<h3 id="t85.1 &#x8BBE;&#x7F6E;cookie">5.1 &#x8BBE;&#x7F6E;cookie <a href="#t85.1 &#x8BBE;&#x7F6E;cookie"> # </a></h3>
<pre><code class="lang-js"> res.cookie(name,value,[,options]);
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">&#x53C2;&#x6570;</th>
<th style="text-align:left">chrome&#x5BF9;&#x5E94;&#x5C5E;&#x6027;</th>
<th style="text-align:left">&#x7C7B;&#x578B;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
<th style="text-align:left">&#x793A;&#x4F8B;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">domain</td>
<td style="text-align:left">Domain</td>
<td style="text-align:left">String</td>
<td style="text-align:left">&#x57DF;&#x540D;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;&#x5F53;&#x524D;&#x57DF;&#x540D;</td>
<td style="text-align:left">{domain:&apos;a.zfpx.cn&apos;}</td>
</tr>
<tr>
<td style="text-align:left">path</td>
<td style="text-align:left">Path</td>
<td style="text-align:left">String</td>
<td style="text-align:left">&#x8DEF;&#x5F84;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;/</td>
<td style="text-align:left">{path:&apos;/visit&apos;}</td>
</tr>
<tr>
<td style="text-align:left">expires</td>
<td style="text-align:left">Expires</td>
<td style="text-align:left">Date</td>
<td style="text-align:left">&#x8FC7;&#x671F;&#x65F6;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x4EE5;&#x6709;&#x6307;&#x5B9A;&#x6216;&#x4E3A;0&#x8868;&#x793A;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#x6709;&#x6548;</td>
<td style="text-align:left">{expires:new Date(Date.now()+20*1000)}</td>
</tr>
<tr>
<td style="text-align:left">maxAge</td>
<td style="text-align:left">Max-Age</td>
<td style="text-align:left">Number</td>
<td style="text-align:left">&#x6709;&#x6548;&#x65F6;&#x95F4;(&#x5355;&#x4F4D;&#x662F;&#x6BEB;&#x79D2;)</td>
<td style="text-align:left">{maxAge:20*1000}</td>
</tr>
<tr>
<td style="text-align:left">httpOnly</td>
<td style="text-align:left">HTTP</td>
<td style="text-align:left">Boolean</td>
<td style="text-align:left">&#x4E0D;&#x80FD;&#x901A;&#x8FC7;&#x6D4F;&#x89C8;&#x5668;javascript&#x8BBF;&#x95EE;</td>
<td style="text-align:left">{httpOnly:true}</td>
</tr>
<tr>
<td style="text-align:left">secure</td>
<td style="text-align:left">Secure</td>
<td style="text-align:left">String</td>
<td style="text-align:left">&#x53EA;&#x901A;&#x8FC7;https&#x534F;&#x8BAE;&#x8BBF;&#x95EE;</td>
</tr>
</tbody>
</table>
<h3 id="t95.2 &#x83B7;&#x53D6;cookie">5.2 &#x83B7;&#x53D6;cookie <a href="#t95.2 &#x83B7;&#x53D6;cookie"> # </a></h3>
<p>&#x4F7F;&#x7528;cookie-parser&#x4E2D;&#x95F4;&#x4EF6;</p>
<pre><code class="lang-js">$ npm install cookie-parser --save
</code></pre>
<pre><code class="lang-js">app.use(<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cookie-parser&apos;</span>)());    <span class="hljs-comment">//&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;</span>
response.cookie(key,value)              <span class="hljs-comment">//&#x5728;&#x54CD;&#x5E94;&#x4E2D;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x8BBE;&#x7F6E;cookie</span>
request.cookies                         <span class="hljs-comment">//&#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x4E2D;&#x7684;cookie&#x5BF9;&#x8C61;</span>
response.clearCookie(<span class="hljs-string">&apos;username&apos;</span>)        <span class="hljs-comment">//&#x6E05;&#x9664;cookie</span>
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> cookieParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cookie-parser&apos;</span>);
<span class="hljs-keyword">var</span> app = express();
<span class="hljs-comment">/**
 * &#x5982;&#x679C;&#x8981;&#x52A0;&#x5BC6;&#x7684;&#x8BDD; cookieParser&#x91CC;&#x8981;&#x6307;&#x5B9A;&#x5BC6;&#x7801;&#xFF0C;&#x800C;&#x4E14;signed&#x8981;&#x7B49;&#x4E8E;true res.cookie(&apos;name&apos;,&apos;zfpx&apos;,{signed:true});
 */</span>
app.use(cookieParser(<span class="hljs-string">&apos;zfpx&apos;</span>));
app.get(<span class="hljs-string">&apos;/write&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    <span class="hljs-comment">//1.&#x666E;&#x901A;&#x8BBE;&#x7F6E;</span>
    <span class="hljs-comment">//res.cookie(&apos;name&apos;,&apos;value&apos;);</span>

    <span class="hljs-comment">//2.&#x8BBE;&#x7F6E;&#x57DF;&#x540D;</span>
    <span class="hljs-comment">//res.cookie(&apos;name&apos;,&apos;zfpx&apos;,{domain:&apos;a.zfpx.cn&apos;});</span>

    <span class="hljs-comment">//3.&#x8BBE;&#x7F6E;&#x8DEF;&#x5F84;</span>
    <span class="hljs-comment">//res.cookie(&apos;name&apos;,&apos;zfpx&apos;,{path:&apos;/visit&apos;});</span>

    <span class="hljs-comment">//4.&#x8FC7;&#x671F;&#x65F6;&#x95F4;</span>
    <span class="hljs-comment">//res.cookie(&apos;name&apos;,&apos;zfpx&apos;,{expires:new Date(Date.now()+20*1000)});//&#x6BEB;&#x79D2;</span>
    <span class="hljs-comment">//res.cookie(&apos;name&apos;,&apos;zfpx&apos;,{maxAge:20*1000});//&#x8FC7;&#x671F;&#x65F6;&#x95F4; &#x6BEB;&#x79D2;</span>

    <span class="hljs-comment">//httpOnly true&#x8FD8;&#x662F;false&#x65E0;&#x610F;&#x4E49; document.cookie&#x53D6;&#x4E0D;&#x5230;</span>
    <span class="hljs-comment">//res.cookie(&apos;name&apos;,&apos;zfpx&apos;,{httpOnly:true});</span>
    res.cookie(<span class="hljs-string">&apos;age&apos;</span>,<span class="hljs-string">&apos;123&apos;</span>,{<span class="hljs-attr">signed</span>:<span class="hljs-literal">true</span>});
    res.end(<span class="hljs-string">&apos;ok&apos;</span>);
});

app.get(<span class="hljs-string">&apos;/read&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    <span class="hljs-built_in">console</span>.log(req.signedCookies);
    res.send(req.cookies);
});

<span class="hljs-comment">//&#x8BB0;&#x5F55;&#x8FD9;&#x662F;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x7B2C;&#x51E0;&#x6B21;&#x8BBF;&#x95EE;</span>
app.get(<span class="hljs-string">&apos;/visit&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.cookie(<span class="hljs-string">&apos;count&apos;</span>,<span class="hljs-built_in">isNaN</span>(req.cookies.count)?<span class="hljs-number">0</span>:<span class="hljs-built_in">parseInt</span>(req.cookies.count)+<span class="hljs-number">1</span>);
    res.send(req.cookies);
});


app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h3 id="t105.3 cookie&#x539F;&#x7406;&#x89E3;&#x6790;">5.3 cookie&#x539F;&#x7406;&#x89E3;&#x6790; <a href="#t105.3 cookie&#x539F;&#x7406;&#x89E3;&#x6790;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cookieParser</span>(<span class="hljs-params">secret</span>)</span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cookieParser</span>(<span class="hljs-params">req, res, next</span>)</span>{
    req.secret = secret;  
    <span class="hljs-keyword">if</span> (!req.headers.cookie) {
        <span class="hljs-keyword">return</span> next();
    }
    req.cookies =  <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;querystring&apos;</span>).parse(req.headers.cookie,<span class="hljs-string">&apos;; &apos;</span>,<span class="hljs-string">&apos;=&apos;</span>);
    <span class="hljs-keyword">if</span>(req.secret){
        req.signedCookies = {};
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> attr <span class="hljs-keyword">in</span> req.cookies){
                <span class="hljs-keyword">let</span> val = req.cookies[attr];
                req.signedCookies[attr] = unsign(val, secret);
            }
        }
    }
    next();
  }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cookie</span>(<span class="hljs-params">name, val, options</span>) </span>{
    <span class="hljs-keyword">var</span> opt = options || {};
    val = <span class="hljs-built_in">encodeURIComponent</span>(val);
    <span class="hljs-keyword">if</span>(opt.secret){
        <span class="hljs-keyword">var</span> secret = <span class="hljs-keyword">this</span>.req.secret;
        val = sign(val,<span class="hljs-keyword">this</span>.secret);
    }

    <span class="hljs-keyword">var</span> pairs = [name + <span class="hljs-string">&apos;=&apos;</span> + value];

    <span class="hljs-keyword">if</span> (<span class="hljs-literal">null</span> != opt.maxAge) {
        <span class="hljs-keyword">var</span> maxAge = opt.maxAge - <span class="hljs-number">0</span>;
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">isNaN</span>(maxAge)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">&apos;maxAge should be a Number&apos;</span>);
        pairs.push(<span class="hljs-string">&apos;Max-Age=&apos;</span> + <span class="hljs-built_in">Math</span>.floor(maxAge));
    }

    <span class="hljs-keyword">if</span> (opt.domain) {
        pairs.push(<span class="hljs-string">&apos;Domain=&apos;</span> + opt.domain);
    }

    <span class="hljs-keyword">if</span> (opt.path) {
        pairs.push(<span class="hljs-string">&apos;Path=&apos;</span> + opt.path);
    }

    <span class="hljs-keyword">if</span> (opt.expires) pairs.push(<span class="hljs-string">&apos;Expires=&apos;</span> + opt.expires.toUTCString());
    <span class="hljs-keyword">if</span> (opt.httpOnly) pairs.push(<span class="hljs-string">&apos;HttpOnly=true&apos;</span>);
    <span class="hljs-keyword">if</span> (opt.secure) pairs.push(<span class="hljs-string">&apos;Secure=true&apos;</span>);

    <span class="hljs-keyword">return</span> pairs.join(<span class="hljs-string">&apos;; &apos;</span>);
}

<span class="hljs-keyword">var</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);
exports.sign = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">val, secret</span>)</span>{
  <span class="hljs-keyword">return</span> val + <span class="hljs-string">&apos;.&apos;</span> + crypto
    .createHmac(<span class="hljs-string">&apos;sha256&apos;</span>, secret)
    .update(val)
    .digest(<span class="hljs-string">&apos;base64&apos;</span>)
    .replace(<span class="hljs-regexp">/\=+$/</span>, <span class="hljs-string">&apos;&apos;</span>);
};

exports.unsign = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">val, secret</span>)</span>{
  <span class="hljs-keyword">var</span> str = val.slice(<span class="hljs-number">0</span>, val.lastIndexOf(<span class="hljs-string">&apos;.&apos;</span>))
    , mac = exports.sign(str, secret);
  <span class="hljs-keyword">return</span> mac == val ? str : <span class="hljs-literal">false</span>;
};

</code></pre>
<h2 id="t116. &#x6743;&#x9650;&#x63A7;&#x5236;">6. &#x6743;&#x9650;&#x63A7;&#x5236; <a href="#t116. &#x6743;&#x9650;&#x63A7;&#x5236;"> # </a></h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> cookieParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cookie-parser&apos;</span>);
<span class="hljs-keyword">var</span> app = express();
app.set(<span class="hljs-string">&apos;view engine&apos;</span>,<span class="hljs-string">&apos;html&apos;</span>);
app.engine(<span class="hljs-string">&apos;html&apos;</span>,<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;ejs&apos;</span>).__express);
app.set(<span class="hljs-string">&apos;views&apos;</span>,__dirname);

app.use(cookieParser());

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkUser</span>(<span class="hljs-params">req,res,next</span>)</span>{
    <span class="hljs-keyword">if</span>(req.cookies &amp;&amp; req.cookies.username)
      next();
    <span class="hljs-keyword">else</span>
      res.redirect(<span class="hljs-string">&apos;/&apos;</span>);
}

<span class="hljs-comment">//&#x8FDB;&#x5165;&#x767B;&#x5F55;&#x9875;</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.render(<span class="hljs-string">&apos;index&apos;</span>);
});

<span class="hljs-comment">//&#x767B;&#x5F55;</span>
app.get(<span class="hljs-string">&apos;/login&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.cookie(<span class="hljs-string">&apos;username&apos;</span>,req.query.username,{<span class="hljs-attr">httpOnly</span>:<span class="hljs-literal">true</span>});
    res.redirect(<span class="hljs-string">&apos;/user&apos;</span>);
});

<span class="hljs-comment">//&#x7528;&#x6237;&#x9875;&#x9762;</span>
app.get(<span class="hljs-string">&apos;/user&apos;</span>,checkUser,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.render(<span class="hljs-string">&apos;user&apos;</span>,{<span class="hljs-attr">username</span>:req.cookies.username});
});

<span class="hljs-comment">//&#x7528;&#x6237;&#x9000;&#x51FA;</span>
app.get(<span class="hljs-string">&apos;/logout&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.clearCookie(<span class="hljs-string">&apos;username&apos;</span>);<span class="hljs-comment">//&#x6E05;&#x9664;cookie</span>
    res.redirect(<span class="hljs-string">&apos;/&apos;</span>);
});

app.listen(<span class="hljs-number">8080</span>);
</code></pre>
<h2 id="t127.cookie&#x4F7F;&#x7528;&#x6CE8;&#x610F;&#x4E8B;&#x9879;">7.cookie&#x4F7F;&#x7528;&#x6CE8;&#x610F;&#x4E8B;&#x9879; <a href="#t127.cookie&#x4F7F;&#x7528;&#x6CE8;&#x610F;&#x4E8B;&#x9879;"> # </a></h2>
<ul>
<li>&#x53EF;&#x80FD;&#x88AB;&#x5BA2;&#x6237;&#x7AEF;&#x7BE1;&#x6539;&#xFF0C;&#x4F7F;&#x7528;&#x524D;&#x9A8C;&#x8BC1;&#x5408;&#x6CD5;&#x6027;</li>
<li>&#x4E0D;&#x8981;&#x5B58;&#x50A8;&#x654F;&#x611F;&#x6570;&#x636E;&#xFF0C;&#x6BD4;&#x5982;&#x7528;&#x6237;&#x5BC6;&#x7801;&#xFF0C;&#x8D26;&#x6237;&#x4F59;&#x989D;</li>
<li>&#x4F7F;&#x7528;httpOnly&#x4FDD;&#x8BC1;&#x5B89;&#x5168;</li>
<li>&#x5C3D;&#x91CF;&#x51CF;&#x5C11;cookie&#x7684;&#x4F53;&#x79EF;</li>
<li>&#x8BBE;&#x7F6E;&#x6B63;&#x786E;&#x7684;domain&#x548C;path&#xFF0C;&#x51CF;&#x5C11;&#x6570;&#x636E;&#x4F20;&#x8F93;</li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
